// pages/demo1/demo1.js

// 形态图片映射表
const formsMap = {
  1: [
    '/pages/demo1/image/皮卡丘.png',
    '/pages/demo1/image/雷丘.png',
    '/pages/demo1/image/阿尔宙斯.png'
  ],
  2: [
    '/pages/demo1/image/妙蛙种子.png',
    '/pages/demo1/image/妙蛙草.png',
    '/pages/demo1/image/妙蛙花.png'
  ],
  3: [
    '/pages/demo1/image/小火龙.png',
    '/pages/demo1/image/火恐龙.png',
    '/pages/demo1/image/喷火龙.png'
  ],
  4: [
    '/pages/demo1/image/杰尼龟.png',
    '/pages/demo1/image/卡咪龟.png',
    '/pages/demo1/image/水箭龟.png'
  ],
  5: [
    '/pages/demo1/image/卡比兽.png',
    '/pages/demo1/image/卡比兽2.png',
    '/pages/demo1/image/卡比兽3.png'
  ],
  6: [
    '/pages/demo1/image/耿鬼.png',
    '/pages/demo1/image/耿鬼2.png',
    '/pages/demo1/image/耿鬼3.png'
  ],
  7: [
    '/pages/demo1/image/波比克.png',
    '/pages/demo1/image/波比克2.png',
    '/pages/demo1/image/波比克3.png'
  ],
  8: [
    '/pages/demo1/image/伊布.png',
    '/pages/demo1/image/伊布2.png',
    '/pages/demo1/image/伊布3.png'
  ],
  9: [
    '/pages/demo1/image/六尾.png',
    '/pages/demo1/image/六尾2.png',
    '/pages/demo1/image/六尾3.png'
  ],
  10: [
    '/pages/demo1/image/胖丁.png',
    '/pages/demo1/image/胖丁2.png',
    '/pages/demo1/image/胖丁3.png'
  ]
};

Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchText: '',
    cardList: [
      { id: 1, name: '皮卡丘', type: '电', image: '/pages/demo0/picture/pkq.jpg', desc: '电气鼠宝可梦', liked: false, price: 299 },
      { id: 2, name: '妙蛙种子', type: '草', image: '/pages/demo0/picture/mwzz.jpg', desc: '草/毒系宝可梦', liked: false, price: 199 },
      { id: 3, name: '小火龙', type: '火', image: '/pages/demo0/picture/xhl.jpg', desc: '火系宝可梦', liked: false, price: 199 },
      { id: 4, name: '杰尼龟', type: '水', image: '/pages/demo0/picture/jng.jpg', desc: '水系宝可梦', liked: false, price: 199 },
      { id: 5, name: '卡比兽', type: '一般', image: '/pages/demo0/picture/kbs.png', desc: '超大型宝可梦', liked: false, price: 399 },
      { id: 6, name: '耿鬼', type: '幽灵', image: '/pages/demo0/picture/ggui.png', desc: '幽灵/毒系宝可梦', liked: false, price: 299 },
      { id: 7, name: '波克比', type: '妖精', image: '/pages/demo0/picture/bkb.jpg', desc: '蛋形宝可梦', liked: false, price: 199 },
      { id: 8, name: '伊布', type: '一般', image: '/pages/demo0/picture/yb.jpg', desc: '进化能力极强', liked: false, price: 299 },
      { id: 9, name: '六尾', type: '火', image: '/pages/demo0/picture/lw.jpg', desc: '拥有六条美丽的尾巴', liked: false, price: 199 },
      { id: 10, name: '胖丁', type: '妖精', image: '/pages/demo0/picture/pd.jpg', desc: '唱歌能让人入睡', liked: false, price: 199 }
    ],
    filteredList: [],
    showModal: false,
    modalPokemon: null,
    modalForms: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ filteredList: this.data.cardList });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 同步收藏状态
    const likedCards = wx.getStorageSync('likedCards') || [];
    const likedIds = likedCards.map(card => card.id);
    const cardList = this.data.cardList.map(card => ({
      ...card,
      liked: likedIds.indexOf(card.id) !== -1
    }));
    this.setData({ cardList });
    this.filterCards(this.data.searchText);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  },

  onSearchInput(e) {
    const value = e.detail.value.trim();
    this.setData({ searchText: value });
    this.filterCards(value);
  },

  filterCards(value) {
    const filtered = this.data.cardList.filter(card =>
      card.name.includes(value) || card.type.includes(value) || card.desc.includes(value)
    );
    this.setData({ filteredList: filtered });
  },

  toggleLike(e) {
    const id = e.currentTarget.dataset.id;
    let cardList = this.data.cardList.map(card =>
      card.id === id ? { ...card, liked: !card.liked } : card
    );
    this.setData({ cardList });
    this.filterCards(this.data.searchText);

    // 同步到本地收藏
    let likedCards = wx.getStorageSync('likedCards') || [];
    const toggledCard = cardList.find(card => card.id === id);
    if (toggledCard.liked) {
      // 添加到收藏
      if (!likedCards.find(item => item.id === toggledCard.id)) {
        likedCards.push(toggledCard);
      }
    } else {
      // 从收藏移除
      likedCards = likedCards.filter(item => item.id !== toggledCard.id);
    }
    wx.setStorageSync('likedCards', likedCards);
  },

  buyCard(e) {
    const id = e.currentTarget.dataset.id;
    const card = this.data.cardList.find(card => card.id === id);

    wx.showModal({
      title: '加入购物车',
      content: `确定要将 ${card.name} 加入购物车吗？价格：¥${card.price}`,
      success: (res) => {
        if (res.confirm) {
          let cartList = wx.getStorageSync('cartList') || []; 
          let existIndex = cartList.findIndex(item => item.id === card.id);
          if (existIndex !== -1) {
            cartList[existIndex].count += 1;
          } else {
            cartList.push({ ...card, count: 1 });
          }
          wx.setStorageSync('cartList', cartList);

          wx.showToast({
            title: '已加入购物车！',
            icon: 'success',
            duration: 2000
          });
        }
      }
    });
  },

  showDetailModal(e) {
    const id = e.currentTarget.dataset.id;
    const pokemon = this.data.cardList.find(item => item.id === id);
    this.setData({
      showModal: true,
      modalPokemon: pokemon,
      modalForms: formsMap[id] || []
    });
  },

  closeModal() {
    this.setData({
      showModal: false
    });
  }
})